#!/bin/bash

#Mariadb Galera Cluster群集
#http://www.cnblogs.com/elvi/p/7736637.html

# ###########################
# ##基本设置
# 请先完成hosts文件和免密登录

# #shell批量配置http://www.cnblogs.com/elvi/p/7681779.html

# ###########################

# #每个节点执行

#配置内核
echo '
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
* soft core 100000
* hard core 100000
'>>/etc/security/limits.conf
#
echo '
fs.file-max=2000000
net.ipv4.ip_local_port_range = 1025 65000  
net.ipv4.tcp_tw_recycle = 1 
'>>/etc/sysctl.conf
sysctl -p
###########################

#MariaDB Galera 安装
yum install mariadb mariadb-server mariadb-galera-server socat -y
yum install expect -y

#SQL root密码
DBPass=innobase

#配置数据库
echo "#
[mysqld]
bind-address = 0.0.0.0
default-storage-engine = innodb
innodb_file_per_table
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8
innodb_buffer_pool_size= 1G
#">/etc/my.cnf.d/openstack.cnf

#启动数据库服务
#systemctl enable mariadb.service
systemctl start mariadb.service

#mysql_secure_installation #初始化设置密码,自动交互
/usr/bin/expect << EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    "password:" { send "$DBPass\r"; exp_continue}
    "new password:" { send "$DBPass\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF

#测试
mysql -u root -p$DBPass -e "show databases;"
[ $? = 0 ] || { echo "mariadb初始化失败";exit; }


#galera配置
/usr/bin/cp /etc/my.cnf.d/galera.cnf{,.bak}
egrep -v "#|^$" /etc/my.cnf.d/galera.cnf.bak >/etc/my.cnf.d/galera.cnf
sed -i 's/wsrep_on=1/wsrep_on=ON/' /etc/my.cnf.d/galera.cnf
sed -i 's/wsrep_sst_auth=root:/wsrep_sst_auth=root:'$DBPass'/' /etc/my.cnf.d/galera.cnf
